package com.sosee.cgfw.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.entity.Data;
import com.sosee.cgfw.myview.PickerView;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;
import com.zcw.togglebutton.ToggleButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thinkpad on 2016/2/25.
 */
public class TicketDetialActivity extends Activity implements ToggleButton.OnToggleChanged {

    private PickerView pv_day;//日期
    private PickerView pv_hour;//小时
    private PickerView pv_min;//分钟
    private PickerView pv_am;//上下午
    private List<String> day_list;//日期集合
    private List<String> hour_list;//小时 集合
    private List<String> time_list;//分集合
    private List<String> am_list;//
    private RelativeLayout ll_time;//事件选择器
    private Data date;
    private TextView tv_date;
    private TextView tv_company;
    private TextView tv_company_e;
    private TextView tv_info;
    private TextView tv_time;//入场时间
    private TextView tv_address;
    private TextView tv_address_e;
    private TextView tv_start_time;//开场时间
    private HttpUtils httpUtils;
    private ToggleButton tb;
    private SharedPreferences sp=null;
    private String day_use;
    private String hour;
    private String min;
    private boolean isPm=false;
    private int position;//接受传递来的位置信息  用于判断详情页的背景
    private View v1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticketdetial);
        setSelect();
        initView();
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    public void setSelect(){
        if(Utils.data==null){
            LogUtils.log_E("data    ======null");
        }else{
            String date=Utils.data.getStart_date();
            String[] da=date.split("-");
            int year=Integer.parseInt(da[0]);
            int mon=Integer.parseInt(da[1]);
            int day=Integer.parseInt(da[2]);
            day_use=Utils.getDate(year,mon,day);
            String time= Utils.data.getStart_time();
            String[] t=time.split(":");
            int t1=Integer.parseInt(t[0]);
            min=t[1];
            if(t1>12){
                isPm=true;
            }
            hour=t1%12+"";
        }


    }


    public void initView(){
        v1=findViewById(R.id.bg);
        //判断 所需的背景
        position=getIntent().getIntExtra("position",1);
        if(position%4==0){
            v1.setBackgroundResource(R.drawable.lvlong);
        }else if(position%3==0){
            v1.setBackgroundResource(R.drawable.zilong);
        }else if(position%2==0){
            v1.setBackgroundResource(R.drawable.lanlong);
        }else{
            v1.setBackgroundResource(R.drawable.fenlong);
        }
        httpUtils=new HttpUtils();
        tb= (ToggleButton) findViewById(R.id.tb);
        tb.setOnToggleChanged(this);
        setToggleStatus();
        ll_time= (RelativeLayout) findViewById(R.id.ll_time);
        tv_info= (TextView) findViewById(R.id.tv_title);
        tv_date= (TextView) findViewById(R.id.tv_date);
        tv_time= (TextView) findViewById(R.id.tv_time);
        tv_address= (TextView) findViewById(R.id.tv_address);
        tv_address_e= (TextView) findViewById(R.id.tv_address_e);
//        tv_company= (TextView) findViewById(R.id.tv_company);
        tv_company_e= (TextView) findViewById(R.id.tv_company_e);
        tv_start_time= (TextView) findViewById(R.id.tv_start_time);
        if(Utils.data==null){
            LogUtils.log_E("utils data ====null");
        }else{
            tv_info.setText(Utils.data.getTitle());
            tv_date.setText(Utils.data.getStart_date());
            tv_time.setText(Utils.data.getEnter_time());
            tv_start_time.setText(Utils.data.getStart_time());
            tv_address.setText(Utils.data.getAddressA());
            tv_address_e.setText(Utils.data.getAddressA_e());
            tv_company_e.setText(Utils.data.getUndertake());
        }


//        tv_company.setText(Utils.data.getUndertake_e());
        initTimerSelectorData();
        initTimerSelector();
    }

    //设置 toggle 初始状态
    public void setToggleStatus(){
        sp=getSharedPreferences(Constans.ALARM, MODE_PRIVATE);
    }

    public void setShare(boolean isRemove,String id){
        String oid=sp.getString(Constans.ALARM, "");
        List<String> temList =Utils.stringToList(oid, ",");
        if(isRemove){
            if(temList.contains(id)){
                temList.remove(id);
            }
        }else{
            temList.add(id);
        }
        oid=Utils.listToString(temList,",");
        SharedPreferences.Editor ed=sp.edit();
        LogUtils.log_E("设置提醒字符串："+oid);
        ed.putString(Constans.ALARM,oid);
        ed.commit();
    }



    public void initTimerSelector() {
        //测试坐标补偿
        buCang();
        pv_day = (PickerView) findViewById(R.id.pv_day);
        pv_day.setmBucang(BUCHANG);
        pv_day.setData(day_list);
        pv_day.setSelected(10);
        pv_hour = (PickerView) findViewById(R.id.pv_hour);
        pv_hour.setData(hour_list);
        pv_hour.setSelected(6);
        pv_min = (PickerView) findViewById(R.id.pv_min);
        pv_min.setData(time_list);
        pv_min.setSelected(10);
        pv_am = (PickerView) findViewById(R.id.pv_am);
        pv_am.setData(am_list);
        if(isPm){
            pv_am.setSelected(1);
        }else{
            pv_am.setSelected(0);
        }


    }

    public void initTimerSelectorData() {
        hour_list = new ArrayList<String>();
        for (int i = 1; i < 13; i++) {
            hour_list.add(i + "时");
        }
        time_list = new ArrayList<String>();
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                time_list.add("0" + i+"分");
            } else {
                time_list.add(i + "分");
            }
        }
        hour_list.add(6,hour+"时");
        time_list.add(10,min+"分");


        //待优化处理
        day_list = Utils.getTime();
//        day_list.add(10,day_use);
        am_list = new ArrayList<String>();
        am_list.add("AM");
        am_list.add("PM");
    }

    public static int BUCHANG;
    public void buCang() {
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int height = metric.heightPixels;   // 屏幕高度（像素）
        int y = height / 2;
        int[] sc = new int[2];
        ll_time.getLocationInWindow(sc);
        int y1 = sc[1] + ll_time.getHeight() / 2;
        BUCHANG = y1 - y;

    }


    //返回
    public void back(View view){
        startActivity(new Intent(this, MyTicketActivity.class));
        finish();
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(this,MyTicketActivity.class));
        finish();
    }
    //删除 邀请函
    public void delete(View view){
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.DELEYAOQING_URL + Utils.data.getID(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtils.log_E(responseInfo.result);
                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                    String status = jsonObject.getString("status");
                    if ("success".equals(status)) {
                        Utils.showToast("删除邀请函成功");
                        startActivity(new Intent(TicketDetialActivity.this, MyTicketActivity.class));
                    } else {
                        Utils.showToast("删除邀请函失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
    //当按钮状态改变时
    @Override
    public void onToggle(boolean on) {
        if(on){
            ll_time.setVisibility(View.VISIBLE);
        }else{
            ll_time.setVisibility(View.INVISIBLE);
            setShare(true, Utils.data.getID());
            Utils.showToast("关闭提前通知");
        }
    }

    public void tixing(View view){
        setShare(false,Utils.data.getID());
        Utils.showToast("开启提前通知");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
